package com.hzvdvmc.autosale.util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william on 2017/12/15.
 */

public class ListUtils {

    public static byte[] listToByte(List<Byte> list) {
        byte[] data = new byte[list.size()];
        for (int i = 0; i < list.size(); i++) {
            data[i] = list.get(i);
        }
        return data;
    }

    public static List<Byte> byteToList(byte[] data) {
        List<Byte> list = new ArrayList<>();
        for (int i = 0; i < data.length; i++) {
            list.add(data[i]);
        }
        return list;
    }

    /**
     * 合并两个数组
     *
     * @param data1
     * @param data2
     * @return
     */
    public static byte[] addByteArray(byte[] data1, byte[] data2) {
        byte[] data = new byte[data1.length + data2.length];
        for (int i = 0; i < data1.length; i++) {
            data[i] = data1[i];
        }
        for (int i = 0; i < data2.length; i++) {
            data[data1.length + i] = data2[i];
        }
        return data;
    }

    /**
     * 合并多个数组
     */
    public static byte[] addByteArray(List<Byte[]> list) {
        int newArraySize = 0;
        for (int i = 0; i < list.size(); i++) {
            newArraySize += list.get(i).length;
        }
        byte[] data = new byte[newArraySize];

        int count = 0;
        for (int i = 0; i < list.size(); i++) {
            Byte[] bytes = list.get(i);
            for (int j = 0; j < bytes.length; j++) {
                data[j + count] = bytes[j];
            }
            count += bytes.length;
        }
        return data;
    }

}
